<#include "/@inc/uiMacros.ftl">
<#include "/@inc/funcMacros.ftl">
<#include "/@inc/copyright.txt">
<#assign className = TriaGram + Prefix + LateType + "Cmd">
<#assign intfName = TriaGram + "I" + Prefix + LateType>
<#assign baseName = TriaGram + Prefix + LateType>
<@pp.renameOutputFile name=className+".cpp">
</@pp.renameOutputFile>

<@CALC_STATES Attributes  />

<#assign numStates = states?size >

//Local Framework
#include "${className}.h"
#include "${baseName}Dlg.h"            // Dialog box to display the inputs

//DialogEngine Framework
#include "CATDialogTransition.h"         // To set NLS resource to the transition
<#if (numStates > 0 ) >
#include "CATFeatureImportAgent.h"
#include "CATDialogAgent .h"
</#if>

//-------------------------------------------------------------------------
// Enables the instanciation of this command by its class name.
// It will be used by the command header to launch the command.
#include "CATCreateExternalObject.h"
CATCreateClass(${className});

//-------------------------------------------------------------------------
// Constructor in Edition Mode (0)
${className}::${className}(CATISpecObject *iObjectToEdit, int 	inCommandMode):
CATPrtPanelStateCmd ("${className}", CATDlgEngOneShot, CATCommandModeExclusive, inCommandMode),
_pDlg(NULL)
{
    HRESULT rc = E_FAIL ;
    _MyFeature = iObjectToEdit ;
<#list states as st>
_pFeatureAgent${st.name}  = NULL ;
_pDialogAgent${st.name}  = NULL ;
</#list>

}

//-------------------------------------------------------------------------
${className}::~${className}()
{
    if ( NULL != _pDlg )
    {
        _pDlg->RequestDelayedDestruction();
        _pDlg = NULL ;
    }


<#list states as st>
if ( NULL != _pFeatureAgent${st.name} )
{
   _pFeatureAgent${st.name}->RequestDelayedDestruction();       _pFeatureAgent${st.name} = NULL ;
}
if ( NULL != _pDialogAgent${st.name} )
{
   _pDialogAgent${st.name}->RequestDelayedDestruction();       _pDialogAgent${st.name} = NULL ;
}
</#list>
}

//-------------------------------------------------------------------------

HRESULT  ${className}::BuildDialog()
{
   _pDlg = new ${baseName}Dlg();
   _pDlg->Build();
   _pDlg->SetOKSensitivity(CATDlgDisable);
   _pDlg->SetPREVIEWSensitivity(CATDlgDisable);

   return S_OK ;
}

//-------------------------------------------------------------------------
void ${className}::BuildGraph()
{
   // All is OK, we can choose new inputs for the user feature
	_pDlg ->SetVisibility(CATDlgShow);

<#if states?size == 0 >
        // This method,from CATMMUIPanelStateCommand, manages the OK, Cancel transitions
        CATDialogState * pInitalState = GetInitialPanelState("InitialState");
<#else>
	CreateStateChart() ;
 </#if>

}

void ${className}::CreateStateChart ()
{

<#if numStates == 1 >
<#assign st = states[0]>
		CATCustomizableState * p${st.name}State = GetInitialPanelState("${st.name}State");
		_pFeatureAgent${st.name} = new CATFeatureAgent("${st.name}Agent");
		 _pFeatureAgent${st.name} -> SetOrderedElementType ( "${st.IID}" );
		_pFeatureAgent${st.name} -> SetBehavior(CATDlgEngWithPSOHSO | CATDlgEngWithPrevaluation);
		_pFeatureAgent${st.name} -> SetAgentBehavior ( MfPermanentBody  | MfLastFeatureSupport | MfRelimitedFeaturization );

		p${st.name}State->AddDialogAgent(_pFeatureAgent${st.name});

		CATDialogTransition * pTransition${st.name}  = AddTransition(p${st.name}State, p${st.name}State,
					  IsOutputSetCondition(_pFeatureAgent${st.name}),
					  Action((ActionMethod) &${className}::OnSelect${st.name}));
<#else>
// 1. Create States and Feature and Dialog Agents
<#list states as st>
		<#if st_index == 0 >
		CATCustomizableState * p${st.name}State = GetInitialPanelState("${st.name}State");
		 <#else>

		  CATCustomizableState * p${st.name}State = AddPanelState("${st.name}State");
		 </#if>

        _pFeatureAgent${st.name} = new CATFeatureAgent("${st.name}Agent");
         _pFeatureAgent${st.name} -> SetOrderedElementType ( "${st.IID}" );
        _pFeatureAgent${st.name} -> SetBehavior(CATDlgEngWithPSOHSO | CATDlgEngWithPrevaluation);
    	_pFeatureAgent${st.name} -> SetAgentBehavior ( MfPermanentBody  | MfLastFeatureSupport | MfRelimitedFeaturization );

        _pDialogAgent${st.name} = new CATDialogAgent("${st.name}Agent");
        _pDialogAgent${st.name} -> AcceptOnNotify ( _pDlg->Get${st.name}List () , _pDlg->Get${st.name}List->GetListSelectNotification() );
</#list>
 </#if>

// 2. Attach Agents to states
<#list states as st>

		// Attach Agents to ${st.name}State
		p${st.name}State->AddDialogAgent(_pFeatureAgent${st.name});
		p${st.name}State->AddDialogAgent(_pDialogAgent${st.name});
		<#list states as stOther>
			<#if st_index != stOther_index >
		p${st.name}State->AddDialogAgent(_pDialogAgent${stOther.name});
		 	</#if>
		</#list>
</#list>

// 3. Add Transitions
<#list states as st>

		<#list states as stOther>
			<#assign diff = stOther_index - st_index >
			<#if diff  == 1>
			// Transition when ${st.name} is selected
			AddTransition( p${st.name}State, p${stOther.name}State,
						  IsOutputSetCondition(_pFeatureAgent${st.name}),
						  Action((ActionMethod) &${className}::OnSelect${st.name}));
		 	</#if>
			<#if st_index != stOther_index >

		// Transition when ${st.name}List is selected
		AddTransition( p${stOther.name}State, p${st.name}State,
					  IsOutputSetCondition(_pDialogAgent${st.name}),
					  Action((ActionMethod) &${className}::OnSelect${st.name}List));
		 	</#if>
		</#list>
</#list>
}

<#list states as st>
${className}::OnSelect${st.name}(void *)
{
    // gets ready for next acquisition
    _pFeatureAgent${st.name}->InitializeAcquisition();

    return TRUE;
}
${className}::OnSelect${st.name}List(void *)
{
    // gets ready for next acquisition
    _pDialogAgent${st.name}->InitializeAcquisition();

    return TRUE;
}
</#list>

<#list states as st>
CATBoolean ${className}::OnSelect${st.name}(void *)
{
    // gets ready for next acquisition
    _pFeatureAgent${st.name}->InitializeAcquisition();

    return TRUE;
}

CATBoolean ${className}::OnSelect${st.name}List(void *)
{
    // gets ready for next acquisition
    _pDialogAgent${st.name}->InitializeAcquisition();

    return TRUE;
}
</#list>


CATBoolean ${className}::SetFeatureInput ()
{
${intfName}_var sp${LateType} = _MyFeature ;

<#list Attributes as attrib>
<#if  (attrib.ui?length > 0 ) >
<#switch attrib.type>

<#case "tk_boolean">
CATBoolean   b${attrib.name} = FALSE  ;
_pDlg->Get${attrib.name}( b${attrib.name} )   ;
sp${LateType}->Set${attrib.name}( b${attrib.name} )   ;
<#break>

<#case "tk_integer">
int   n${attrib.name} = 0  ;
_pDlg->Get${attrib.name}( n${attrib.name} )   ;
sp${LateType}->Set${attrib.name}( n${attrib.name} )   ;
<#break>

<#case "tk_double">
double   d${attrib.name} = 0  ;
_pDlg->Get${attrib.name}( d${attrib.name} )   ;
sp${LateType}->Set${attrib.name}( d${attrib.name} )   ;
<#break>

<#case "tk_string">
CATUnicodeString   str${attrib.name}   ;
_pDlg->Get${attrib.name}( str${attrib.name} )   ;
sp${LateType}->Set${attrib.name}( str${attrib.name} )   ;
<#break>
</#switch>
</#if>
</#list>

    return TRUE ;
}